import pygame
import random

# 初始化参数
WIDTH, HEIGHT = 800, 700  # 增加高度给底部区域
CARD_SIZE = 80
ROWS = 5
COLS = 7
CARD_TYPES = 6
SLOT_AREA_HEIGHT = 120  # 底部区域高度

# 颜色定义
COLORS = [
    (255, 0, 0),  # 红
    (0, 255, 0),  # 绿
    (0, 0, 255),  # 蓝
    (255, 255, 0),  # 黄
    (255, 0, 255),  # 紫
    (0, 255, 255)  # 青
]


class Card:
    def __init__(self, card_type, row, col, layer):
        self.card_type = card_type
        self.row = row
        self.col = col
        self.layer = layer
        self.selected = False


class Game:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption("消除游戏")

        # 初始化卡片堆
        self.grid = [[[] for _ in range(COLS)] for _ in range(ROWS)]
        for layer in range(3):
            for row in range(ROWS):
                for col in range(COLS):
                    if random.random() < 0.7:
                        card_type = random.randint(0, CARD_TYPES - 1)
                        self.grid[row][col].append(Card(card_type, row, col, layer))

        # 底部暂存区参数
        self.slot_rect = pygame.Rect(0, HEIGHT - SLOT_AREA_HEIGHT, WIDTH, SLOT_AREA_HEIGHT)
        self.slot_positions = [
            (WIDTH // 2 - CARD_SIZE * 1.5 - 20, HEIGHT - SLOT_AREA_HEIGHT // 2 - CARD_SIZE // 2),
            (WIDTH // 2 - CARD_SIZE // 2, HEIGHT - SLOT_AREA_HEIGHT // 2 - CARD_SIZE // 2),
            (WIDTH // 2 + CARD_SIZE * 0.5 + 20, HEIGHT - SLOT_AREA_HEIGHT // 2 - CARD_SIZE // 2)
        ]

        self.selected = []
        self.game_over = False

    def get_top_card(self, row, col):
        if self.grid[row][col]:
            return self.grid[row][col][-1]
        return None

    def draw_card(self, card, x, y):
        color = COLORS[card.card_type]
        if card.selected:
            pygame.draw.rect(self.screen, (255, 255, 255), (x - 2, y - 2, CARD_SIZE + 4, CARD_SIZE + 4))
        pygame.draw.rect(self.screen, color, (x, y, CARD_SIZE, CARD_SIZE))
        pygame.draw.rect(self.screen, (0, 0, 0), (x, y, CARD_SIZE, CARD_SIZE), 1)

    def draw_slot_area(self):
        # 绘制底部背景
        pygame.draw.rect(self.screen, (180, 180, 180), self.slot_rect)

        # 绘制卡槽边框
        for pos in self.slot_positions:
            pygame.draw.rect(self.screen, (100, 100, 100),
                             (pos[0] - 2, pos[1] - 2, CARD_SIZE + 4, CARD_SIZE + 4), 2)

        # 绘制已选卡片
        for i, card in enumerate(self.selected):
            if i >= 3: break
            x, y = self.slot_positions[i]
            self.draw_card(card, x, y)

    def check_match(self):
        if len(self.selected) != 3:
            return False
        return all(c.card_type == self.selected[0].card_type for c in self.selected)

    def remove_cards(self):
        for card in self.selected:
            self.grid[card.row][card.col].pop()
        self.selected = []

    def check_win(self):
        return all(not cell for row in self.grid for cell in row)

    def run(self):
        running = True
        while running:
            self.screen.fill((200, 200, 200))

            # 事件处理
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

                if event.type == pygame.MOUSEBUTTONDOWN and not self.game_over:
                    x, y = event.pos
                    col = (x - 100) // (CARD_SIZE + 10)
                    row = (y - 100) // (CARD_SIZE + 10)

                    if 0 <= row < ROWS and 0 <= col < COLS:
                        card = self.get_top_card(row, col)
                        if card and len(self.selected) < 3:
                            card.selected = not card.selected
                            if card.selected:
                                self.selected.append(card)
                            else:
                                self.selected.remove(card)

                    # 自动检查消除
                    if len(self.selected) == 3:
                        if self.check_match():
                            self.remove_cards()
                            if self.check_win():
                                self.game_over = True
                        else:
                            for c in self.selected:
                                c.selected = False
                            self.selected = []

            # 绘制主游戏区卡片
            for layer in range(3):
                for row in range(ROWS):
                    for col in range(COLS):
                        cards = self.grid[row][col]
                        if len(cards) > layer:
                            card = cards[layer]
                            pos_x = 100 + col * (CARD_SIZE + 10)
                            pos_y = 100 + row * (CARD_SIZE + 10) - layer * 5
                            self.draw_card(card, pos_x, pos_y)

            # 绘制底部暂存区
            self.draw_slot_area()

            # 胜利提示
            if self.game_over:
                font = pygame.font.SysFont('simhei', 72)
                text = font.render('游戏胜利！', True, (0, 200, 0))
                self.screen.blit(text, (WIDTH // 2 - 140, HEIGHT // 2 - 50))

            pygame.display.flip()

        pygame.quit()


if __name__ == "__main__":
    game = Game()
    game.run()